package us.mooresr.webtroopmanager.client.activity;

import java.util.ArrayList;
import java.util.List;

import us.mooresr.webtroopmanager.client.ClientFactory;
import us.mooresr.webtroopmanager.client.TroopManagerService;
import us.mooresr.webtroopmanager.client.TroopManagerServiceAsync;
import us.mooresr.webtroopmanager.client.place.EventParticipantsPlace;
import us.mooresr.webtroopmanager.client.ui.EventParticipantsView;
import us.mooresr.webtroopmanager.shared.EventParticipantSummary;
import us.mooresr.webtroopmanager.shared.PersonSummary;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class EventParticipantsActivity extends MyActivity implements EventParticipantsView.Presenter {

	
	
	public EventParticipantsActivity(EventParticipantsPlace place,
			ClientFactory clientFactory) {
		super(place, clientFactory);
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		super.start(panel, eventBus);
		
		EventParticipantsView view = clientFactory.getEventParticipantsView();
		view.setPresenter(this);
		loadData(); // Asynchronous call to get the data for the view
		panel.setWidget(view.asWidget());
		
	}

	@Override
	public Long getEventId() {
		return ((EventParticipantsPlace)place).getEventId();
	}
	
	private void loadData() {
		/*
		 * Start an asynchronous call to load up the data for the view
		 * The "place" has information on what event we're viewing
		 * When the data has been retrieved the callbacks will tell the view
		 * to update itself.
		 */
		
		TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);

		AsyncCallback<List<PersonSummary>> personCallback = new AsyncCallback<List<PersonSummary>>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<PersonSummary> result) {
				clientFactory.getEventParticipantsView().setPotentialParticipants(result);
			}
		};
		
		AsyncCallback<List<EventParticipantSummary>> participantCallback = new AsyncCallback<List<EventParticipantSummary>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(List<EventParticipantSummary> result) {
				clientFactory.getEventParticipantsView().setCurrentParticipants(result);
				
			}
			
		};

		troopManagerService.getAllPeople(personCallback);
		troopManagerService.getEventParticipants(((EventParticipantsPlace)place).getEventId(), participantCallback);
		
		
	}

	@Override
	public void storeEventParticipants(ArrayList<EventParticipantSummary> participants) {
		TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
		Long eventId = ((EventParticipantsPlace)place).getEventId();
		
		AsyncCallback<Long> callback = new AsyncCallback<Long>() {

			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
				
			}

			@Override
			public void onSuccess(Long result) {
				// TODO Auto-generated method stub
				
			}
			
		};
		
		troopManagerService.setEventParticipants(eventId, participants, callback);
		
		
		
	}




	@Override
	public PersonSummary getPerson(Long personId) {
		// TODO Auto-generated method stub
		return null;
	}


}
